/**
 * OpenAPI 生成的类型辅助工具
 * 
 * 此文件提供从生成的 OpenAPI 类型中提取常用类型的辅助工具
 * 使用方法：import { ApiPaths, GetResponse, PostResponse } from '@/types/api-generated'
 */

import type { paths } from './generated'

// API 路径类型
export type ApiPaths = paths

// 提取 GET 请求的响应类型
export type GetResponse<
  Path extends keyof paths,
  Method extends 'get' = 'get'
> = paths[Path][Method] extends {
  responses: {
    200: {
      content: {
        'application/json': infer R
      }
    }
  }
}
  ? R
  : never

// 提取 POST 请求的响应类型
export type PostResponse<Path extends keyof paths> = paths[Path]['post'] extends {
  responses: {
    200: {
      content: {
        'application/json': infer R
      }
    }
  }
}
  ? R
  : never

// 提取 PUT 请求的响应类型
export type PutResponse<Path extends keyof paths> = paths[Path]['put'] extends {
  responses: {
    200: {
      content: {
        'application/json': infer R
      }
    }
  }
} ? R : never

// 提取 DELETE 请求的响应类型
export type DeleteResponse<Path extends keyof paths> = paths[Path]['delete'] extends {
  responses: {
    200: {
      content: {
        'application/json': infer R
      }
    }
  }
} ? R : never

// 提取 PATCH 请求的响应类型
export type PatchResponse<Path extends keyof paths> = paths[Path]['patch'] extends {
  responses: {
    200: {
      content: {
        'application/json': infer R
      }
    }
  }
}
  ? R
  : never

// 提取请求体类型
export type RequestBody<
  Path extends keyof paths,
  Method extends 'post' | 'put' | 'patch' = 'post'
> = paths[Path][Method] extends {
  requestBody: {
    content: {
      'application/json': infer R
    }
  }
}
  ? R
  : never

// 提取查询参数类型
export type QueryParams<Path extends keyof paths> = paths[Path]['get'] extends {
  parameters: {
    query: infer Q
  }
}
  ? Q
  : never

// 提取路径参数类型
export type PathParams<Path extends keyof paths> = paths[Path]['get'] extends {
  parameters: {
    path: infer P
  }
}
  ? P
  : never

// 统一响应类型（后端使用 ApiResponse 包装）
export type ApiResponse<T = any> = {
  success: boolean
  code: number
  message: string
  data?: T
  error?: {
    type: string
    message: string
    details?: any
  }
  timestamp: string
  request_id?: string
}

// 分页响应类型
export type PaginatedResponse<T = any> = ApiResponse<{
  items: T[]
  pagination: {
    total: number
    page: number
    size: number
    pages: number
    has_prev: boolean
    has_next: boolean
  }
}>

/**
 * 常用的 API 路径常量
 * 使用这些常量可以确保类型安全
 */
export const API_PATHS = {
  // 认证相关
  AUTH: {
    LOGIN: '/api/v1/auth/login',
    REGISTER: '/api/v1/auth/register',
    REFRESH_TOKEN: '/api/v1/auth/refresh-token',
    LOGOUT: '/api/v1/auth/logout',
    ME: '/api/v1/auth/me',
  },
  // 用户相关
  USERS: {
    LIST: '/api/v1/users',
    COUNT: '/api/v1/users/count',
    DETAIL: (id: string) => `/api/v1/users/${id}`,
    CREATE: '/api/v1/users',
    UPDATE: (id: string) => `/api/v1/users/${id}`,
    DELETE: (id: string) => `/api/v1/users/${id}`,
  },
  // 项目相关
  PROJECTS: {
    LIST: '/api/v1/projects',
    COUNT: '/api/v1/projects/count',
    DETAIL: (id: string) => `/api/v1/projects/${id}`,
    CREATE: '/api/v1/projects',
    UPDATE: (id: string) => `/api/v1/projects/${id}`,
    DELETE: (id: string) => `/api/v1/projects/${id}`,
  },
  // 任务相关
  TASKS: {
    LIST: '/api/v1/tasks',
    COUNT: '/api/v1/tasks/count',
    DETAIL: (id: string) => `/api/v1/tasks/${id}`,
    CREATE: '/api/v1/tasks',
    UPDATE: (id: string) => `/api/v1/tasks/${id}`,
    DELETE: (id: string) => `/api/v1/tasks/${id}`,
  },
} as const
